/**
 * Created by Administrator on 2017/4/7.
 */
function game1_round1() {
    var bgp= new Image();
    var fight_plane_x=450;
    var fight_plane_y=450;
    var key_down=null;
    var pressed=false;//用于keyUp前，keyDown不能重复执行
    var fight_plane_move_siv;
    bgp.src="images/bgp_game1_round1.jpg";
    var is_bgp_loaded=setInterval(function () {
        //console.log("加载中");
        //进行背景是否加载完成判断
        if(bgp.width!=0){
            clearInterval(is_bgp_loaded);
            draw_bg_move();
            //console.log("加载完成");
            draw_fight_plane(fight_plane_x,fight_plane_y);

            // W A S D
            document.body.onkeydown=function (e) {
                //console.log(e.which);
                //e.which==87&&draw_fight_plane(fight_plane_x,fight_plane_y--);
                //document.body.onkeydown=null;
                key_down=e.which;

                if  ( key_down == 87 && pressed == false ){
                    fight_plane_move_siv=setInterval(function () {
                        fight_plane_y=fight_plane_y-1;
                        draw_fight_plane(fight_plane_x,fight_plane_y);
                    },1)
                }

                if(key_down==83 && pressed == false){
                    fight_plane_move_siv=setInterval(function () {
                        fight_plane_y=fight_plane_y+1;
                        draw_fight_plane(fight_plane_x,fight_plane_y);
                    },1)
                }

                if(key_down==65&&pressed==false){
                    fight_plane_move_siv = setInterval( function ( ) {
                        fight_plane_x=fight_plane_x-1;
                        draw_fight_plane(fight_plane_x,fight_plane_y);
                    },1)
                }

                if(key_down==68&&pressed==false){
                    fight_plane_move_siv = setInterval( function ( ) {
                        fight_plane_x=fight_plane_x+1;
                        draw_fight_plane(fight_plane_x,fight_plane_y);
                    },1)
                }
                //抬起取消移动
                pressed=true;
                document.body.onkeyup=function (e) {
                    if(e.which==key_down){
                        clearInterval(fight_plane_move_siv);
                        pressed=false;
                    }
                }
            }
        }
    },1);

    function draw_bg_move() {
        var bgp_pos_top=0;
        og.style.background="#000 url('images/bgp_game1_round1.jpg')";
        og.style.backgroundSize="1000px 600px";
        setInterval(function () {
            og.style.backgroundPosition="0 "+bgp_pos_top+++"px";
            //bgp_pos_top==0&&(bgp_pos_top=1000);
        },10);
    }
}
function draw_fight_plane(x,y) {
 if(0<x<1000){
     //console.log(x+' '+y);
     ogc.globalCompositeOperation="copy";  //异或结合处理,清除被覆盖的图型
     ogc.beginPath();
     ogc.moveTo(x,y+4);
     ogc.lineTo(x-10,y+12);
     ogc.lineTo(x-16,y+25);
     ogc.lineTo(x-16,y+65);
     ogc.lineTo(x+16,y+65);
     ogc.lineTo(x+16,y+25);
     ogc.lineTo(x+10,y+12);
     ogc.fillStyle="#aaa";
     ogc.fill();
     ogc.closePath();
     ogc.beginPath();
     ogc.globalCompositeOperation="destination-over";
     ogc.moveTo(x,y+25);
     ogc.lineTo(x-25,y+40);
     ogc.lineTo(x-25,y+60);
     ogc.lineTo(x-16,y+65);
     ogc.lineTo(x+16,y+65);
     ogc.lineTo(x+25,y+60);
     ogc.lineTo(x+25,y+40);
     ogc.fillStyle="#666";
     ogc.fill();
     ogc.closePath();
 }
}